<template>
  <div class="container">
    <div class="app-container">
      <!-- element树形组件 -->
      <el-tree
        :data="depts"
        :props="defaultProps"
        default-expand-all
        :expand-on-click-node="false"
      >
        <!-- 使用作用域插槽进行批量渲染 -->
        <template v-slot="{ data }">
          <!-- 组件的节点结构 -->
          <el-row
            type="flex"
            justify="space-between"
            align="middle"
            style="width: 100%; height: 40px"
          >
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="tree-manager">{{ data.managerName }}</span>
              <!-- 塞一个下拉菜单 -->
              <el-dropdown @command="operateDept($event, data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right"></i>
                </span>
                <!-- 下拉菜单选项 -->
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="delete">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <!-- 放置弹窗 -->
    <add-dept
      ref="addDept"
      @updataDepartment="getDepartment"
      :currentNodeId="currentNodeId"
      :showDialog.sync="showDialog"
    ></add-dept>
  </div>
</template>

<script>
import { delDepartment, getDepartment } from "@/api/department";
import { transListToTreeData } from "@/utils";
import addDept from "./components/add-dept.vue";

export default {
  name: "Department",
  components: {
    addDept,
  },
  data() {
    return {
      //控制弹窗变量
      showDialog: false,
      //具体点击的组件id
      currentNodeId: null,
      depts: [
        {
          name: "传智教育",
          managerName: "管理员",
          children: [
            { name: "总裁办", managerName: "张三" },
            { name: "行政部", managerName: "管理员" },
            { name: "人事部", managerName: "管理员" },
          ],
        },
      ],
      defaultProps: {
        label: "name", //树形组件的名称
        children: "children", //树形的每一个子节点
      },
    };
  },
  methods: {
    //1.获取组织方法
    async getDepartment() {
      const result = await getDepartment();
      this.depts = transListToTreeData(result, 0);
    },
    //2.部门操作方法
    operateDept(type, id) {
      if (type === "add") {
        this.showDialog = true;
        this.currentNodeId = id;
      } else if (type === "edit") {
        this.showDialog = true;
        this.currentNodeId = id;
        //props父传子是异步动作，调用子组件方法时，props还未更新，所以id为null
        //解决办法：使用$nextTick，等待异步操作执行完成后调用
        this.$nextTick(() => {
          //父组件调用子组件的方法
          this.$refs.addDept.getDepartmentDetail();
        });
      } else {
        this.$confirm("您确定要删除该部门吗?").then(async () => {
          await delDepartment(id);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getDepartment();
        });
      }
    },
  },
  created() {
    this.getDepartment();
  },
};
</script>

<style>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}
.tree-manager {
  width: 50px;
  display: inline-block;
  margin: 30px;
}
</style>